package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsUnit;
import net.realsee.domain.wcs.data.WcsUnitCharact;
import net.realsee.domain.wcs.data.bo.WcsUnitCharactBo;
import net.realsee.domain.wcs.data.vo.WcsUnitCharactVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsUnitCharactMapper;
import net.realsee.mapper.wcs.data.WcsUnitMapper;
import net.realsee.service.wcs.data.IWcsUnitCharactService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Unit特性Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsUnitCharactServiceImpl extends ServiceImpl<WcsUnitCharactMapper, WcsUnitCharact> implements IWcsUnitCharactService {

    private final WcsUnitCharactMapper baseMapper;

    private final WcsUnitMapper wcsUnitMapper;

    /**
     * 查询Unit特性
     */
    @Override
    public WcsUnitCharactVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询Unit特性列表
     */
    @Override
    public TableDataInfo<WcsUnitCharactVo> selectVoPage(WcsUnitCharactBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<WcsUnitCharact> lqw = buildVoQueryWrapper(bo);
//        Page<WcsUnitCharactVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        IPage<WcsUnitCharactVo> result = baseMapper.selectWcsUnitCharactVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询Unit特性列表
     */
    @Override
    public List<WcsUnitCharactVo> selectVoList(WcsUnitCharactBo bo) {
        LambdaQueryWrapper<WcsUnitCharact> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsUnitCharact> buildVoQueryWrapper(WcsUnitCharactBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsUnitCharact> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsUnitCharact::getId, bo.getId());
        lqw.eq(bo.getUnitId() != null, WcsUnitCharact::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getCharact()), WcsUnitCharact::getCharact, bo.getCharact());
        lqw.eq(bo.getUpperLimit() != null, WcsUnitCharact::getUpperLimit, bo.getUpperLimit());
        lqw.eq(bo.getLowerLimit() != null, WcsUnitCharact::getLowerLimit, bo.getLowerLimit());
        lqw.eq(bo.getTargetValue() != null, WcsUnitCharact::getTargetValue, bo.getTargetValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAttribute()), WcsUnitCharact::getAttribute, bo.getAttribute());
        lqw.eq(bo.getRequired() != null, WcsUnitCharact::getRequired, bo.getRequired());
        lqw.eq(bo.getControlUpperLimit() != null, WcsUnitCharact::getControlUpperLimit, bo.getControlUpperLimit());
        lqw.eq(bo.getControlLowerLimit() != null, WcsUnitCharact::getControlLowerLimit, bo.getControlLowerLimit());
        lqw.eq(StringUtils.isNotBlank(bo.getUomCode()), WcsUnitCharact::getUomCode, bo.getUomCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsUnitCharact::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsUnitCharact::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsUnitCharact::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getDelFlag() != null, WcsUnitCharact::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsUnitCharact::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsUnitCharact::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsUnitCharact::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsUnitCharact::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsUnitCharact::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsUnitCharact::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsUnitCharact::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsUnitCharact::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增Unit特性
     */
    @Override
    public Boolean insertByBo(WcsUnitCharactBo bo) {
        WcsUnitCharact add = BeanUtil.toBean(bo, WcsUnitCharact.class);
        return this.save(add);
    }

    /**
     * 修改Unit特性
     */
    @Override
    public Boolean updateByBo(WcsUnitCharactBo bo) {
        WcsUnitCharact update = BeanUtil.toBean(bo, WcsUnitCharact.class);
        return this.updateById(update);
    }

    /**
     * 批量删除Unit特性
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    @Override
    public boolean addUnitCharact(WcsUnitCharactBo bo) {
        Long needAddUnitId = bo.getNeedAddUnitId();
        String tableName = bo.getTableName();
        // 通过传入的id查询需要添加特性的数据 unitId 是否为空
        Long unitId = baseMapper.selectDataByIdAndTableName(needAddUnitId, tableName);

        if (unitId == null) {
            // 生成10位唯一 unitId
            Random random = new Random();
            unitId = (long) ((random.nextDouble() * 9 + 1) * 1000000000);

            // 将unit_id存入 unit表
            WcsUnit wcsUnit = new WcsUnit();
            wcsUnit.setUnitId(unitId);
            this.wcsUnitMapper.insert(wcsUnit);

            // 若unitId为空 则在该表对应这条数据添加上unitId
            baseMapper.updateUnitIdByIdAndTableName(needAddUnitId, tableName, unitId);

            // 将其他数据一起添加进unit特性表
            WcsUnitCharact add = BeanUtil.toBean(bo, WcsUnitCharact.class);
            // 将生成的unit_id set到 Unit特性表
            add.setUnitId(unitId);
            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setUnitId(add.getUnitId());
            }
            return flag;
        } else {
            // 将其他数据一起添加成一条新数据
            WcsUnitCharact add = BeanUtil.toBean(bo, WcsUnitCharact.class);
            add.setUnitId(unitId);
            boolean flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setUnitId(add.getUnitId());
            }
            return flag;
        }
    }
}
